import { Component, OnInit } from '@angular/core'
import { Router } from '@angular/router'
import { QlMessageService } from 'qloud-angular'
import {DictService} from "@shared/utils/dict.service";
import {ApiService} from "../../services/api.service";
import {SearchComponent} from "../../components/search/search.component";
import {ModalService} from "@shared/utils";
import {SearchTagsComponent} from "../../components/search-tags/search-tags.component";
import {DictPipe} from "@shared/pipes/dict.pipe";
import {FormBuilder, FormGroup} from "@angular/forms";
import {CursorService} from "@core/http/cursor.service";

@Component({
  selector: 'app-tag-search',
  templateUrl: './tag-search.component.html',
  styleUrls: ['./tag-search.component.scss'],
  providers: [ModalService,DictPipe]
})
export class TagSearchComponent implements OnInit {
  public formGroup: FormGroup = this.fb.group({})
  tagname =''//标签名称
  tagtype =''//标签类型
  tagexplain =''//标签说明
  calogTag =[];//标签目录列表
  catalogcode = ''//目录名称
  scene = ''//应用场景
  tagcode  =''
  tagsList = [];
  constructor(private router: Router,
              private message: QlMessageService,
              public dictService: DictService,
              private apiService: ApiService,
              private fb: FormBuilder,
              public modal: ModalService,
              public cursorService: CursorService,) {}

  ngOnInit() {
    this.formGroup = this.fb.group({ valuedata: [''] })
    let sql = 'select *'
    this.onRefresh(sql)
  }
  transform(key:string,value:string,sql:string){
    if(sql != 'select *'){
      sql += ' and ' + key +" = " + "'*" + value + "*'"
    }
    else{
      sql += " where " + key +" = " + "'*" + value + "*'";
    }
    return sql
  }
  search(){
    this.modal
      .open({
        size: 'large',
        title: '高级搜索',
        component: SearchTagsComponent,
        data: {}
      })
      .subscribe((sql) => {
        console.log('sql:'+sql)
        this.onRefresh(sql)
      })
  }
  onRefresh(sql){
    if(this.tagname !=''){
      sql= this.transform('tagname',this.tagname ,sql)
    }
    if(this.tagcode !=''){
      sql= this.transform('tagcode',this.tagcode ,sql)
    }
    this.cursorService.statement =sql;
    this.cursorService.limit =10;
    this.apiService.getTagList(this.cursorService).subscribe(data => {
      this.tagsList = data;

    })
  }
  // onSubmit() {
  //   this.router.navigate(['/customer/customer-tag'], { replaceUrl: true })
  //   this.message.success('共查询到5个标签')
  // }
  submit() {
    let data = {'manageAtrr':{}}
    if(this.catalogcode!=''){
      data['manageAtrr']['catalogcode'] =this.catalogcode
    }
    if(this.tagname!=''){
      data['manageAtrr']['tagname'] =this.tagname
    }
    if(this.tagtype!=''){
      data['manageAtrr']['tagtype'] =this.tagtype
    }
    if(this.tagexplain!=''){
      data['manageAtrr']['tagexplain'] =this.tagexplain
    }
    if(this.scene!=''){
      data['manageAtrr']['scene'] =this.scene
    }
    if(this.tagcode!=''){
      data['manageAtrr']['tagcode'] =this.tagcode
    }
    // return true
    return data
  }
}
